define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        user_models = require('js/apps/console/models'),
        utils = require('js/utils/tools'),
        tz = require('js/utils/tz'),
        cookie = require('js/utils/cookie'),
        app = Backbone.history;
    require('datetimepicker');
    require('multiselect');
    require('magnific');
    require('jquery-ui');

    var MetisOrderListView = Backbone.View.extend({
        events: {
            'click #exportData': 'exportData',
            'click #search': 'doSearch',
        },

        initialize: function (options) {
            this.options = options;
            this.collection = new models.MetisOrderCollection();
            this.user_collection = new user_models.UserCollection();
            this.checkedIds = [];
            this.collection.bind('change remove reset', this.renderWithData, this);
            this.uuid = "";
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        exportData: function () {
            notify.warning('警告', '导出中，请等待');
            utils.exportData(this.collection.url, this._getSearchOption());
        },
        _getSearchOption: function () {
            var options = {};
            var searched = utils.getAllInput('#sidebar_right');
            searched.game_id = parseInt($('#searchGame').val());
            _.each(['game_id'], function (k) {
                if (searched[k] != -1) options[k] = searched[k];
            })
            if (searched.ticket_id) {
                options.ticket_id = searched.ticket_id;
            }
            if (searched.user_id) {
                options.user_id = searched.user_id;
            }
            if (searched.create_time) {
                options.create_time = searched.create_time;
            }
            utils.saveSearched('op/metis_order', searched);
            return options;
        },
        doSearch: function () {
            this.doRefresh(true);
        },
        doRefresh: function (useSearch) {
            var options = {};
            if (useSearch) {
                options = this._getSearchOption();
            }
            options.$page = 1;
            options.$size = 300;
            options.$orderby = "-create_time";
            options._ = new Date().getTime();
            app.navigate(utils.composeQueryString('#op/metis_order/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                total_bet: this.collection.total_bet,
                total_win: this.collection.total_win,
                searched: utils.getSearched('op/metis_order')
            }));

            var that = this;

            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'ticket_id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/metis_order/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('.datetime-input').datetimepicker({
                format: 'YYYY-MM-DD HH:mm:ss',
            });
            $('.date-input').datetimepicker({
                format: 'YYYY-MM-DD',
            });
            utils.getPaginator(that.options, this.collection.total, '#op/metis_order/');

            return this;
        },
        load: function () {
            this.user_collection.fetch({
                reset: true,
                data: {"$size": 300},
                error: function (c, r, o) {
                    notify.notifyResp(r);
                }
            });
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                    $('#content').append('<h4 class="text-muted">无数据</h4>');
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/MetisOrder.html",
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            this.options.$size = 300;
            var view = new MetisOrderListView(this.options);
            view.template = this.template;
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();

            return this;
        }
    });

});
